import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'other_logic.dart';

class OtherPage extends StatelessWidget {
  OtherPage({super.key});

  final OtherLogic logic = Get.put(OtherLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('todolist')),
      body: Column(
        children: [
          Padding(
            padding: EdgeInsets.all(12),
            child: Row(
              children: [
                Expanded(
                  flex: 2,
                  child: TextField(
                    controller: logic.inputController,
                    decoration: InputDecoration(
                      hintText: '输入任务',
                      border: OutlineInputBorder(),
                    ),
                    onSubmitted: (_) => logic.addItem(),
                    textInputAction: TextInputAction.done, // 控制键盘右下角“完成”按钮
                  ),
                ),
                SizedBox(width: 10),
                ElevatedButton(
                  onPressed: () {
                    logic.addItem();
                  },
                  child: Text('add+'),
                ),
              ],
            ),
          ),
          // ===== 全选行 =====
          Obx(() {
            // 三态：全选 = true；部分选中 = null；全不选 = false
            final triValue =
                logic.isAllSelected
                    ? true
                    : (logic.hasAnySelected ? null : false);
            return Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12),
              child: Row(
                children: [
                  Checkbox(
                    tristate: true,
                    value: triValue,
                    onChanged: (v) => logic.toggleAll(v),
                  ),
                  const Text('全选'),
                  const Spacer(),
                  Text(
                    '已完成 ${logic.todos.where((e) => e.done).length}'
                    ' / 共 ${logic.todos.length}',
                  ),
                ],
              ),
            );
          }),
          Expanded(
            child: Obx(
              () => ListView.builder(
                itemBuilder: (ctx, index) {
                  final item = logic.todos[index];
                  return ListTile(
                    leading: Checkbox(
                      activeColor: Colors.green,
                      value: item.done,
                      onChanged: (_) => logic.toggleDone(item.id),
                    ),
                    title: Text(
                      item.title,
                      style: TextStyle(
                        decoration:
                            item.done
                                ? TextDecoration.lineThrough
                                : TextDecoration.none,
                        color: item.done ? Colors.green : Colors.black,
                      ),
                    ),
                    trailing: IconButton(
                      onPressed: () {
                        logic.delItem(item.id);
                      },
                      icon: Icon(Icons.delete, color: Colors.red),
                    ),
                  );
                },
                itemCount: logic.todos.length,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
